package im.yisme.view.ui.animation;

import java.util.ArrayList;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.Log;
import im.yisme.R;
import im.yisme.view.ui.animation.pojo.ComboPOJO;

public class Combo extends Thread{

	private ComboPOJO combo;
	private int realCombo;
	private static final int POLLING_INTERVAL = 100;
	private static final int ANIMATION_UPDATE_INTERVAL = 5;
	private static final int ANIMATION_ONCE_TIMES = 20;
	private int count = 0;
	private boolean isRunning;
	
	private Bitmap num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;
	
	private static final int X = 285;
	private static final int Y = 250;
	
	public Combo(ComboPOJO combo, Resources res){
		initBitmap(res);
		
		this.combo = combo;
		count = 0;
		realCombo = 0;
		isRunning = true;
	}
	
	public void run(){
		
		while(isRunning){
			try {
						
			if(combo.getCombo() >= realCombo) {
				combo.setCombo(realCombo);
				Thread.sleep(POLLING_INTERVAL);
				continue;
			}
			
			if(combo.getCombo() == 0){
				combo.getComboList().add(num0);
				combo.getComboPointList().add(new PointF(X-num0.getWidth()/2, Y-num0.getHeight()/2));
			}
			
			while(count < ANIMATION_ONCE_TIMES){
				
				if(count<10){
					float scale = (num0.getWidth()+(float)count) / num0.getWidth();
					
					int size = combo.getComboList().size();
					
					
					for(int i=0;i<size;i++){
						int temp1 =  (combo.getCombo() % (int)(Math.pow(10, i+1)));
						int num = temp1 / (int)Math.pow(10, i);
						Bitmap temp = zoomIn(getNumBitmap(num), scale);
						combo.getComboList().set(i, temp);
						PointF point = new PointF(X - temp.getWidth()/2	- i*50, Y - temp.getHeight()/2);
						combo.getComboPointList().set(i, point);
					}
				}
				else {
					combo.setAlpha(combo.getAlpha()-10);
				}
				
				count++;
				Thread.sleep(ANIMATION_UPDATE_INTERVAL);
			}
			
			combo.setCombo(combo.getCombo()+1);
			setBitmap(combo.getCombo());
			combo.setAlpha(255);
			count = 0;
			
			
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	public void stopRunning(){
		isRunning = false;
	}
	public void setRealCombo(int combo){
		this.realCombo = combo;
	}
	private Bitmap zoomIn(Bitmap input,float scale){
		
		Matrix matrix = new Matrix();
	
        matrix.postScale(scale, scale);
            
        Bitmap output = Bitmap.createScaledBitmap(input, (int)(input.getWidth()*scale), (int)(input.getHeight()*scale), false);
        
        
        return output;
	}
	private void initBitmap(Resources res){
		num0 = BitmapFactory.decodeResource(res, R.drawable.combo0);
		num1 = BitmapFactory.decodeResource(res, R.drawable.combo1);
		num2 = BitmapFactory.decodeResource(res, R.drawable.combo2);
		num3 = BitmapFactory.decodeResource(res, R.drawable.combo3);
		num4 = BitmapFactory.decodeResource(res, R.drawable.combo4);
		num5 = BitmapFactory.decodeResource(res, R.drawable.combo5);
		num6 = BitmapFactory.decodeResource(res, R.drawable.combo6);
		num7 = BitmapFactory.decodeResource(res, R.drawable.combo7);
		num8 = BitmapFactory.decodeResource(res, R.drawable.combo8);
		num9 = BitmapFactory.decodeResource(res, R.drawable.combo9);
	}
	private Bitmap getNumBitmap(int num){

			switch (num) {
			case 0:
				return num0;
			case 1:
				return num1;
			case 2:
				return num2;
			case 3:
				return num3;
			case 4:
				return num4;
			case 5:
				return num5;
			case 6:
				return num6;
			case 7:
				return num7;
			case 8:
				return num8;
			case 9:
				return num9;
			default:
				break;
			}
		
		return null;
		
	}
	private void setBitmap(int num){
		combo.getComboList().clear();
		combo.getComboPointList().clear();
		
		int count = 0;
		while(num>0){
			int reminder = num % 10;
			num = num / 10;
			
			switch (reminder) {
			case 0:
				combo.getComboList().add(num0);
				break;
			case 1:
				combo.getComboList().add(num1);
				break;
			case 2:
				combo.getComboList().add(num2);
				break;
			case 3:
				combo.getComboList().add(num3);
				break;
			case 4:
				combo.getComboList().add(num4);
				break;
			case 5:
				combo.getComboList().add(num5);
				break;
			case 6:
				combo.getComboList().add(num6);
				break;
			case 7:
				combo.getComboList().add(num7);
				break;
			case 8:
				combo.getComboList().add(num8);
				break;
			case 9:
				combo.getComboList().add(num9);
				break;
			
			default:
				break;
			}
			
			combo.getComboPointList().add(new PointF(X - num0.getWidth()/2 - count*50, Y - num0.getHeight()/2));
		    count++;
		}
	}
}
